/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.fatecmm.cadastrotimes.servlets;

import br.edu.fatecmm.cadastrotimes.banco.LigasDAO;
import br.edu.fatecmm.cadastrotimes.beans.BnLigas;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Douglas
 */
public class SltLigas extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        operacao = request.getParameter("operacao");
        chave = request.getParameter("chave");

        RequestDispatcher rd = null;
        
        if("incluir".equals(operacao))
        {
            rd = request.getRequestDispatcher("/incluirLigas.jsp");
        }
        else if("gravarinclusao".equals(operacao))
        {
            bnLigas = new BnLigas();
            bnLigas.setDs_ligas(request.getParameter("ds_ligas"));

            try
            {
                ligasDAO = new LigasDAO();
                ligasDAO.salvar(bnLigas);

                rd = request.getRequestDispatcher("SltLigas?operacao=listar");
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        else if("editar".equals(operacao))
        {
            try
            {
                bnLigas = new BnLigas();
                ligasDAO = new LigasDAO();
                bnLigas = ligasDAO.procurar(Integer.parseInt(chave));
                HttpSession session = request.getSession(true);
                session.setAttribute("bnLigas", bnLigas);
                rd = request.getRequestDispatcher("/alterarLigas.jsp");
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        else if("gravaralteracao".equals(operacao))
        {
            bnLigas = new BnLigas();
            bnLigas.setId_ligas(Integer.parseInt(request.getParameter("id_ligas")));
            bnLigas.setDs_ligas(request.getParameter("ds_ligas"));
            
            try
            {
                ligasDAO = new LigasDAO();
                ligasDAO.atualizar(bnLigas);

                rd = request.getRequestDispatcher("SltLigas?operacao=listar");
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        else if("listar".equals(operacao))
        {
            try
            {
                ligasDAO = new LigasDAO();
                List itens = ligasDAO.todos();
                request.setAttribute("itens", itens);
                rd = request.getRequestDispatcher("/ligasListar.jsp");
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        else if("excluir".equals(operacao))
        {
            try
            {
                bnLigas = new BnLigas();
                ligasDAO = new LigasDAO();
                bnLigas.setId_ligas(Integer.parseInt(chave));
                ligasDAO.excluir(bnLigas);
                rd = request.getRequestDispatcher("StlLigas?operacao=listar");
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        

        if(rd != null)
            rd.forward(request, response);

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private String operacao;
    private String chave;
    private BnLigas bnLigas;
    private LigasDAO ligasDAO;

}
